/********************************************************************************
 * Project Name		[JavaSE_Fundamentals]
 * File Name     	[TestCalendar.java]
 * Creation Date 	[2015-01-01]
 * 
 * Copyright© ge.y.yang@gmail.com All Rights Reserved
 * 
 * Work hard, play harder, think big and keep fit
 ********************************************************************************/
package api.util;

import static java.util.Calendar.AM_PM;
import static java.util.Calendar.DATE;
import static java.util.Calendar.HOUR_OF_DAY;
import static java.util.Calendar.MINUTE;
import static java.util.Calendar.MONTH;
import static java.util.Calendar.SECOND;
import static java.util.Calendar.YEAR;

import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Locale;
import java.util.TimeZone;

/**
 * java.util.Calendar测试
 * 
 * @author 不落的太阳(Sean Yang aka ShortPeace)
 * @version 1.0
 * @since jdk 1.8
 * 
 */
public class TestCalendar {

	/**
	 * 获得本机所有可以的Locale
	 * 
	 * @return
	 */
	public Locale[] getAvailableLocales() {
		return Calendar.getAvailableLocales();
	}

	/**
	 * 打印日期
	 * 
	 * @param calendar
	 *            待打印的日期
	 */
	public static void printDate(Calendar calendar) {
		String date = calendar.getTimeZone().getDisplayName() + "\t" + calendar.get(YEAR) + "-"
				+ (calendar.get(MONTH) + 1) + "-" + calendar.get(DATE) + " " + calendar.get(HOUR_OF_DAY) + ":"
				+ calendar.get(MINUTE) + ":" + calendar.get(SECOND) + " " + (calendar.get(AM_PM) == 0 ? "上午" : "下午");
		System.out.println(date);
	}

	public static void main(String[] args) {
		Calendar calendar = Calendar.getInstance();
		printDate(calendar);

		calendar.set(2015, Calendar.DECEMBER, 03);
		printDate(calendar);

		// java.util.Calendar和java.util.Date相互转换
		java.util.Date date = calendar.getTime();
		System.out.println("java.util.Date = " + date);

		calendar.setTime(new java.util.Date());
		printDate(calendar);

		// java.util.GregorianCalendar是Calendar的子类
		GregorianCalendar calendar01 = new GregorianCalendar(2015, Calendar.MARCH, 24);
		printDate(calendar01);
		GregorianCalendar calendar02 = new GregorianCalendar(TimeZone.getDefault(), Locale.getDefault());
		printDate(calendar02);
	}
}
